package com.econ.powercloud.ems.timing.compute.strategy.process;

import com.econ.powercloud.ems.common.enums.ComputeType;
import com.econ.powercloud.ems.common.enums.DitTypeEnum;
import com.econ.powercloud.ems.dal.entity.EmsComputeConfig;
import com.econ.powercloud.ems.pojo.FlashDataVO;
import com.econ.powercloud.ems.pojo.QueryDataVO;
import com.econ.powercloud.ems.service.common.QueryCalDataCommonService;
import com.econ.powercloud.ems.service.common.QueryFlashDataCommonService;
import com.econ.powercloud.ems.timing.compute.strategy.AbstractComputeCodeProcess;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author : zjf
 * @Description : 除法操作
 * @Date : 2022/11/25 16:05
 * @Modified by : zjf
 */
@Component
public class DivisionComputeCodeProcess extends AbstractComputeCodeProcess {
    public DivisionComputeCodeProcess() {
        setComputeUnitId(ComputeType.DIVISION.getCode());
    }

    @Autowired
    private QueryFlashDataCommonService queryFlashDataCommonService;
    @Autowired
    private QueryCalDataCommonService queryCalDataCommonService;

    @Override
    public Double compute(EmsComputeConfig config, Map<String, Double> hasCalculateVal,Long startTime) {
        if(CollectionUtils.isEmpty(config.getCalCode()) && config.getConstant() == null){
            return null;
        }
        if(CollectionUtils.isEmpty(config.getCalCode())){
            return null;
        }
        //遥测遥信数据
        List<QueryDataVO> ycYxQueryDataList = getQueryDataListByCalCodeList(config.getCalCode().stream().filter(d-> !hasCalculateVal.containsKey(d.getPid()) && DitTypeEnum.isFlashData(d.getDitType())).collect(Collectors.toList()));
        //计算遥测遥信数据
        List<QueryDataVO> calQueryDataList = getQueryDataListByCalCodeList(config.getCalCode().stream().filter(d-> !hasCalculateVal.containsKey(d.getPid()) && DitTypeEnum.isCalData(d.getDitType())).collect(Collectors.toList()));
        List<FlashDataVO> flashDataVOList = this.queryFlashDataCommonService.getFlashData(ycYxQueryDataList);
        Map<String,FlashDataVO> flashDataMap = flashDataVOList.stream().collect(Collectors.toMap(FlashDataVO::getPid,d->d,(p1,p2)->p1));
        List<FlashDataVO> calDataVOList = this.queryCalDataCommonService.getCalData(calQueryDataList);
        Map<String,FlashDataVO> calDataMap = calDataVOList.stream().collect(Collectors.toMap(FlashDataVO::getPid,d->d,(p1,p2)->p1));
        //默认第一个值为被除数，其他都为除数
        Double result = this.getCalCodeVal(config.getCalCode().get(0),hasCalculateVal,flashDataMap,calDataMap);
        if(result == null){
            return null;
        }
        for (int i=1; i<config.getCalCode().size(); i++){
            Double subtract = this.getCalCodeVal(config.getCalCode().get(i),hasCalculateVal,flashDataMap,calDataMap);
            if(subtract == null){
                //当计算点号的值有一个不存在时，不进行计算
                return null;
            }
            result = result / subtract;
        }
        if(config.getConstant() != null){
            result = result / config.getConstant();
        }
        return keepDecimalPoint(config.getDecimalNumber(),result);
    }
}
